<?php
// +----------------------------------------------------------------------
// | Description: 系统用户
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;



use think\Db;
use app\admin\model\Common;

class BidFile extends ApiCommon
{

    public function index()
    {
        $Model = model('BidFile');
        $param = $this->param;
        if(empty($param['bid_id'])){
            $this->error("致命错误 code：0x00005");
        }
        $data = $Model->where(array('bid_id'=>$param['bid_id']))->select();
        if (!$data) {
            return resultArray(['error' => $Model->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    public function save()
    {
        $Model = model('BidFile');
        $param = $this->param;
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: POST');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        $file = request()->file('file');
        if (!$file) {
            return resultArray(['error' => '请上传文件']);
        }
        if(empty($param['bid_id'])){
            return resultArray(['error' => '致命错误 code：0x00006']);
        }
        $info = $file->validate(['ext' => 'jpg,png,xlsx,doc,pdf,txt,xls'])->move(ROOT_PATH . DS . 'uploads');
        if ($info) {
            $data['bid_id']=$param['bid_id'];
            $data['attachment']='uploads'. DS .$info->getSaveName();
            $data['type']=$param['type'];
            $data['title']=$param['title'];
            $data['content']=$param['content'];
            $data=$Model->save($data);
            if(!$data){
                return resultArray(['error' => '保存失败']);
            }
        }else{
            return resultArray(['error' => '文件格式不正确，致命错误 code：0x00007']);
        }
        return resultArray(['data' => '添加成功']);
    }


    public function deletes()
    {
        $Model = model('BidFile');
        $param = $this->param;
        $data = $Model->delDatas($param['ids']);
        if (!$data) {
            return resultArray(['error' => $Model->getError()]);
        }
        return resultArray(['data' => '删除成功']);
    }
    public function deletesDefine()
    {
        $Model = model('BidFileDefine');
        $param = $this->param;
        $data = $Model->delDatas($param['ids']);
        if (!$data) {
            return resultArray(['error' => $Model->getError()]);
        }
        return resultArray(['data' => '删除成功']);
    }
    public function getDefine()
    {
        $Model = model('BidFileDefine');
        $param = $this->param;
        if(empty($param['bid_id'])){
            return resultArray(['error' => '致命错误 code：0x00006']);
        }
        $data=$Model->where(array('bid_id'=>$param['bid_id']))->select();
        return resultArray(['data' => $data]);
    }
    public function saveDefine()
    {
        $Model = model('BidFileDefine');
        $param = $this->param;
        $arr=[];
        $upArr=[];
        foreach ($param as $k=>$v){
            if(!empty($v['id'])){
                $upArr[]=$v;
                continue;
            };
            $arr[]=$v;
        }
        $data = $Model->insertAll($arr);
        foreach ($upArr as $k=>$v){
            $Model->updateDataById($v,$v['id']);
        }

        return resultArray(['data' => '添加成功']);
    }
}
